﻿using System;
using System.Collections.Generic;
using System.Linq;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using Dapper;
using Ultimus.WFServer;
using AkazoNobel.Ultimus.Extension;

namespace AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service
{
    public static class MdcRecipientService
    {
        public static IEnumerable<Variable> SetNewStoreRecipient(string province, string region)
        {
            //格式化省份格式，去掉“省”，“市”， “自治区”
            province = province.Replace("省", "").Replace("市", "").Replace("自治区", "");
            
            var variables = new List<Variable>();
            var recA = new SortedList<string, string>();

            List<MdcRecipient> recS;
            using (var conn = SqlConnectionHelper.GetConnection(Constant.MDC_DB_NAME))
            {
                recS = conn.Query<MdcRecipient>(
                    "SELECT * FROM mdc_Recipient WHERE process=@process AND status=1",
                    new { process = "New Store Application" }).ToList();
            }

            foreach (var rec in recS)
            {
                if (string.IsNullOrEmpty(rec.Distribute)) continue;
                var distributes = rec.Distribute.Split('_');
                var regionA = distributes[0].Trim();
                var provinceA = distributes.Length > 1 ? distributes[1].Trim() : "*";

                //判断省份
                if (!provinceA.Contains("*") && !provinceA.Contains(province))
                {
                    continue;
                }

                //判断区域
                if (!regionA.Contains("*") && !String.Equals(regionA, region, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                if (recA.ContainsKey(rec.Step))
                {
                    if (!recA.Values[recA.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                    {
                        recA[rec.Step] = recA.Values[recA.IndexOfKey(rec.Step)] +
                                         (rec.Type == "EMAIL" ? ";" : ",") +
                                         rec.Recipient;
                    }
                }
                else
                {
                    recA.Add(rec.Step, rec.Recipient);
                }
            }

            //步骤处理人赋值
            for (int i = 0; i < recA.Keys.Count; i++)
            {
                variables.Add("R_" + recA.Keys[i].Replace(" ", "_"), recA.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
            }

            return variables;
        }

        /// <summary>
        /// 主数据变更步骤处理人
        /// </summary>
        public static IEnumerable<Variable> SetStoreDataChangeRecipient(string region, string province, string category)
        {
            var variables = new List<Variable>();
            var recA = new SortedList<string, string>();

            List<MdcRecipient> recS;
            using (var conn = SqlConnectionHelper.GetConnection(Constant.MDC_DB_NAME))
            {
                recS = conn.Query<MdcRecipient>(
                    "SELECT * FROM mdc_Recipient WHERE process=@process AND status=1",
                    new { process = "Store Data Change" }).ToList();
            }

            foreach (var rec in recS)
            {
                if (string.IsNullOrEmpty(rec.Distribute)) continue;
                var distributes = rec.Distribute.Split('_');
                var regionA = distributes[0];
                var categoryA = distributes.Length > 1 ? distributes[1] : "*";
                var provinceA = distributes.Length > 2 ? distributes[2] : "*";

                //判断区域
                if (!regionA.Contains("*") && !String.Equals(regionA, region, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                //判断变更种类
                if (!categoryA.Contains("*") && !String.Equals(categoryA, category, StringComparison.CurrentCultureIgnoreCase))
                {
                    continue;
                }

                //判断省份
                if (!provinceA.Contains("*") && !provinceA.Contains(province))
                {
                    continue;
                }

                if (recA.ContainsKey(rec.Step))
                {
                    if (!recA.Values[recA.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                    {
                        recA[rec.Step] = recA.Values[recA.IndexOfKey(rec.Step)] +
                                         (rec.Type == "EMAIL" ? ";" : ",") +
                                         rec.Recipient;
                    }
                }
                else
                {
                    recA.Add(rec.Step, rec.Recipient);
                }
            }

            //步骤处理人赋值
            for (int i = 0; i < recA.Keys.Count; i++)
            {
                variables.Add("R_" + recA.Keys[i].Replace(" ", "_"), recA.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
            }

            return variables;
        }
    }
}